package me.lwn.auth.resources;

import me.lwn.auth.application.RegisteredClientManager;
import me.lwn.auth.common.HttpMsg;
import me.lwn.auth.common.PageRequest;
import me.lwn.auth.common.PageResponse;
import me.lwn.auth.domain.Client;
import me.lwn.auth.resources.cmd.RegisteredClientCommand;
import me.lwn.auth.resources.vo.RegisteredClientVo;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("client")
public class ClientController {

    private final RegisteredClientManager manager;

    public ClientController(RegisteredClientManager manager) {
        this.manager = manager;
    }

    @GetMapping("init")
    public RegisteredClientVo beforeAdd() {
        return manager.init();
    }

    @PostMapping
    public RegisteredClientVo save(@RequestBody @Valid RegisteredClientCommand cmd) {
        return manager.save(cmd);
    }

    @GetMapping("{id}")
    public RegisteredClientVo view(@PathVariable String id) {
        return manager.get(id);
    }

    @DeleteMapping("{id}")
    public HttpMsg<String> delete(@PathVariable("id") String id) {
        manager.delete(id);
        return HttpMsg.success("删除成功");
    }

    @GetMapping("client-authentication-method")
    public Map<String, String> clientAuthenticationMethod() {
        return new HashMap<String, String>() {{
            put("client_secret_basic", "BASIC");
            put("client_secret_post", "POST");
            put("none", "");
        }};
    }

    @GetMapping("authorization-grant-type")
    public Map<String, String> authorizationGrantType() {
        return new HashMap<String, String>() {{
            put("authorization_code", "认证码模式");
            put("password", "密码模式");
            put("refresh_token", "刷新token");
            put("client_credentials", "客户端模式");
        }};
    }

    @PostMapping("page")

    public PageResponse<Client> page(@RequestBody PageRequest params) {
        return manager.page(params);
    }
}
